/*
 * Android Runner is a multiplayer GPS game fully written by "Xurxo Mendez Perez"
 * 
 * Copyright (C) 2010 "Xurxo Mendez Perez"
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.androidrunner.view.web.ws.game;

import java.io.IOException;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;

import es.sonxurxo.androidrunner.model.persistence.util.exceptions.DuplicateInstanceException;
import es.sonxurxo.androidrunner.model.persistence.util.exceptions.InstanceNotFoundException;
import es.sonxurxo.androidrunner.model.service.game.GameService;
import es.sonxurxo.androidrunner.model.service.game.to.MessageTO;
import es.sonxurxo.androidrunner.view.web.application.AndroidRunnerApplication;
import es.sonxurxo.androidrunner.view.web.session.AndroidRunnerSession;
import es.sonxurxo.androidrunner.view.web.ws.util.ExceptionCodes;
import es.sonxurxo.androidrunner.view.xml.ServletUtils;
import es.sonxurxo.androidrunner.view.xml.TOToXMLConversor;

/**
 * @author "Xurxo Mendez Perez"
 *
 */
public class SendMessageWS extends WebPage {

	public SendMessageWS(PageParameters pageParameters) {
		
		String login = AndroidRunnerSession.get().getLoginResultTO().getLogin();
		String receiverLogin = pageParameters.getString("receiverLogin");
		String body = pageParameters.getString("body");
		int typeOfMessage;
		if (receiverLogin == null)
			typeOfMessage = MessageTO.PLAYER_BROADCAST_MESSAGE;
		else
			typeOfMessage = MessageTO.PLAYER_PERSONAL_MESSAGE;
		
		MessageTO messageTO = new MessageTO(login, receiverLogin, body, typeOfMessage);

		try {
			
			GameService gameService = AndroidRunnerApplication.get().getGameService();
			gameService.playerSendsMessage(login, messageTO);
			
			ServletUtils.writeServiceResponse(
					TOToXMLConversor.toXML(
							TOToXMLConversor.OK_MESSAGE_SENT_ELEMENT_NAME), this.getResponse());
			
		} catch (InstanceNotFoundException e) {
			try {
				ServletUtils.writeServiceResponse(TOToXMLConversor.toXML(e,
						ExceptionCodes.INSTANCE_NOT_FOUND_CODE), this.getResponse());
			} catch (IOException io) {				
				System.out.println("IOIO : " + e.getMessage());			
			}
		} catch (DuplicateInstanceException e) {
			try {
				ServletUtils.writeServiceResponse(TOToXMLConversor.toXML(e,
						ExceptionCodes.DUPLICATE_INSTANCE_CODE), this.getResponse());
			} catch (IOException io) {				
				System.out.println("IOIO : " + e.getMessage());			
			}
		} catch (Exception e) {
			try {
				ServletUtils.writeServiceResponse(TOToXMLConversor.toXML(e,
						ExceptionCodes.GENERAL_CODE), this.getResponse());
			} catch (IOException io) {				
				System.out.println("IOIO : " + e.getMessage());			
			}
		}
	}

}
